<?php
declare (strict_types = 1);

namespace app\command;

use think\cache\driver\Redis;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Config;
use think\facade\Db;

class VersionMake extends Command
{
    public $table_name = 'app_version';
    protected function configure()
    {
        // 指令配置
        $this->setName('app\command\versionmake')
            ->setDescription('创建版本控制数据表');    
    }

    protected function execute(Input $input, Output $output)
    {
    	// 指令输出
        $output->writeln(date('Y-m-d H:i:s'));
        $output->writeln(date('创建版本控制数据表'));
        $this->handle();
        $output->writeln(date('Y-m-d H:i:s'));
    }

    public function handle()
    {   
        if ($this->isExistVTable() && !$this->outInfo()) {
            return null;
        }

        $this->createVTable();
    }

    /**
     * 获取sql语句
     * @return string
     */
    public function createSql()
    {
        return <<<EOT
                CREATE TABLE `{$this->table_name}` (
                    `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增id',
                    `platform_id` tinyint(1) unsigned NOT NULL COMMENT '1:安卓 2:苹果',
                    `version_code` varchar(20) NOT NULL COMMENT '版本号',
                    `version_name` varchar(32) NOT NULL DEFAULT '' COMMENT '版本名称',
                    `apk_url` varchar(512) NOT NULL DEFAULT '' COMMENT '更新包地址',
                    `content` text NOT NULL COMMENT '更新内容(换行用\\n)',
                    `force_update` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '0:非强更 1:强更',
                    `is_push` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '是否推送更新(1:推送 2:不推送)',
                    `push_time` datetime NOT NULL DEFAULT '1000-01-01 00:00:00' ON UPDATE CURRENT_TIMESTAMP COMMENT '更新推送时间',
                    `add_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间',
                    `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                    PRIMARY KEY (`id`) USING BTREE,
                    UNIQUE KEY `channel_id` (`platform_id`,`version_code`) USING BTREE
                ) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='APP更新包地址';
EOT;    
    }

    /**
     * 创建数据表
     */
    public function createVTable()
    {
        $this->clearVTable();
        $res = Db::execute($this->createSql());
        echo "创建成功\n";
        
    }

    /**
     * 清除数据表
     */
    public function clearVTable()
    {
        $sql = "DROP TABLE IF EXISTS `{$this->table_name}`;";
        $res = Db::execute($sql);
        echo "清除成功\n";
        
    }

    /**
     * 判断数据表是否存在
     * @return bool
     */
    public function isExistVTable()
    {
        // $sql = "select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_NAME='app_version';";
        $sql = "show tables like '{$this->table_name}'";
        var_dump($sql);
        $res = Db::query($sql);
        return empty($res) ? false : true;
    }

    /**
     * 脚本输出提示
     * @return bool
     */
    public function outInfo()
    {
        
        $fh = fopen('php://stdin', 'r');  
        echo "++++++++++++++++++++++++++++++++++++\n";
        echo "+++++  [{$this->table_name}] 已存在!  ++++++\n";
        echo "++++++++++++++++++++++++++++++++++++\n";
        $this->formatOut("是否覆盖此前数据库(0:否 1:是)");
        $str = fread($fh, 1000);
        $res = $str == 1 ? true : false;
        fclose($fh);
        return $res;
    }

    /**
     * 格式化输出
     * @param String $str
     */
    public function formatOut(String $str)
    {
        if (!empty($str)) {
            echo "\033[38;5;1;4m".$str."\033[0m\n";
        }
    }
}
